package service;

import mapper.HardDiskMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import po.ComboboxItem;
import po.HardDisk;
import po.Result;

import java.util.ArrayList;
import java.util.List;

@Service
public class HardDiskServiceImpl implements HardDiskService {
    @Autowired
    private HardDiskMapper hardDiskMapper;

    @Override
    public List<HardDisk> findAllHardDisks(HardDisk hardDisk) throws Exception {
        return hardDiskMapper.findAllHardDisks(hardDisk);
    }

    @Override
    public Result addHardDisk(HardDisk hardDisk) {
        Result result = new Result();
        try {
            hardDiskMapper.addHardDisk(hardDisk);
        } catch (Exception e) {
            result.setResult("fail");
            result.setMessage("添加失败！");
            return result;
        }
        result.setResult("success");
        result.setMessage("添加成功！");
        return result;
    }

    @Override
    public Result deleteHardDiskById(int id) {
        Result result = new Result();
        try {
            hardDiskMapper.deleteHardDiskById(id);
        } catch (Exception e) {
            result.setResult("fail");
            result.setMessage("删除失败！");
            return result;
        }
        result.setResult("success");
        result.setMessage("删除成功！");
        return result;
    }

    @Override
    public List<ComboboxItem> findHardDiskForComb() throws Exception {
        List<HardDisk> hardDiskList = hardDiskMapper.findAllHardDisks(null);
        List<ComboboxItem> list = new ArrayList<>();
        for (HardDisk hardDisk : hardDiskList) {
            ComboboxItem item = new ComboboxItem();
            item.setId(hardDisk.getId());
            item.setText(hardDisk.toString());
            list.add(item);
        }
        return list;
    }

    @Override
    public Result updateHardDiskById(int id, HardDisk hardDisk) {
        Result result = new Result();
        try {
            hardDisk.setId(id);
            hardDiskMapper.updateHardDiskById(hardDisk);
        } catch (Exception e) {
            result.setResult("fail");
            result.setMessage("修改失败！");
            return result;
        }
        result.setResult("success");
        result.setMessage("修改成功！");
        return result;
    }
}
